package main

import (
	"fmt"
	"sync"
)

var (
	wg  = sync.WaitGroup{}
	num = 1
)

func f1(chan1 chan int) {
	defer wg.Done()
	for {
		x, ok := <-chan1
		if !ok {
			break
		}
		fmt.Printf("协程1——%v \n", x)
		if x >= 20 {
			if chan1 != nil {
				close(chan1)
			}
			break
		}
		chan1 <- x + 1
	}
}

func f2(chan1 chan int) {
	defer wg.Done()
	for {
		x, ok := <-chan1
		if !ok {
			break
		}
		fmt.Printf("协程2——%v \n", x)
		if x >= 20 {
			if chan1 != nil {
				close(chan1)
			}
			break
		}
		chan1 <- x + 1
	}
}

func main() {
	chan1 := make(chan int, 1)
	chan1 <- 1
	wg.Add(2)
	go f1(chan1)
	go f2(chan1)
	wg.Wait()
}
